package com.macllvm.whale.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.util.Log;
import com.macllvm.whale.ui.AboutActivity;
import com.macllvm.whale.ui.widget.SettingEditPanel;
import com.macllvm.whale.WhaleApplication;
import com.macllvm.whale.protocol.ProtocolController;
import com.macllvm.whale.settings.SystemSettings;
import com.macllvm.whale.inter.SaveCallback;
import com.macllvm.whale.entity.Buddy;
import com.macllvm.whale.R;

public class SettingsFragment extends Fragment {

    private View mSettingHost;
    private View mSettingNickName;
    private View mSettingSignature;
    private View mSettingAbout;
    private TextView mHostText;
    private TextView mNickNameText;
    private TextView mSignatureText;
    private TextView mDownloadText;
    private SettingEditPanel mEditPanel;
    private SystemSettings systemSettings;
    private SettingsSaveCallback mSettingsSaveCallback = new SettingsSaveCallback();
    private View mView;

    @Override
    public void onAttach (Activity activity) {
        super.onAttach(activity);

        WhaleApplication app = (WhaleApplication)activity.getApplication();
        systemSettings = app.getSystemSettings();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(null == mView) {
            mView = inflater.inflate(R.layout.settings_fragment, container, false);

            mSettingAbout = mView.findViewById(R.id.settings_about_item);
            mSettingHost = mView.findViewById(R.id.settings_host_item);
            mSettingNickName = mView.findViewById(R.id.settings_nick_item);
            mSettingSignature = mView.findViewById(R.id.settings_signature_item);

            mHostText = (TextView) mView.findViewById(R.id.settings_host_name);
            mNickNameText = (TextView) mView.findViewById(R.id.settings_nick_name);
            mSignatureText = (TextView) mView.findViewById(R.id.settings_signature);
            mDownloadText = (TextView) mView.findViewById(R.id.settings_download_path);

            mHostText.setText((String) systemSettings.getSettingValue(SystemSettings.KEY_NAME[0]));
            mNickNameText.setText((String) systemSettings.getSettingValue(SystemSettings.KEY_NAME[1]));
            mSignatureText.setText((String) systemSettings.getSettingValue(SystemSettings.KEY_NAME[2]));
            mDownloadText.setText((String) systemSettings.getSettingValue(SystemSettings.KEY_NAME[3]));

            mEditPanel = (SettingEditPanel) mView.findViewById(R.id.setting_edit_panel);
            mEditPanel.addCallback(mSettingsSaveCallback);
        }else{
            ViewGroup parent = (ViewGroup)mView.getParent();
            if (null != parent) {
                parent.removeView(mView);
            }
        }
        return mView;
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSettingAbout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent intent = new Intent(getActivity(), AboutActivity.class);
            startActivity(intent);
            }
        });

        mSettingHost.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            String key = SystemSettings.KEY_NAME[0];
            String val = (String)systemSettings.getSettingValue(key);
            String title = systemSettings.getItemTitle(key);
            mEditPanel.setItemKeyValue(key, val, title);
            mEditPanel.show();
            }
        });

        mSettingNickName.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            String key = SystemSettings.KEY_NAME[1];
            String val = (String)systemSettings.getSettingValue(key);
            String title = systemSettings.getItemTitle(key);
            mEditPanel.setItemKeyValue(key, val, title);
            mEditPanel.show();
            }
        });

        mSettingSignature.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            String key = SystemSettings.KEY_NAME[2];
            String val = (String)systemSettings.getSettingValue(key);
            String title = systemSettings.getItemTitle(key);
            mEditPanel.setItemKeyValue(key, val, title);
            mEditPanel.show();
            }
        });

    }


    class SettingsSaveCallback implements SaveCallback{
        @Override
        public void onSave(String key, Object val) {
            String value = (String)val;
            systemSettings.setSettingValue(key, val);
            systemSettings.saveSettings(key, val);
            Buddy buddy = ProtocolController.getInstance().getBuddyManager().get(systemSettings.getLocalIP());

            if(SystemSettings.KEY_NAME[0].equals(key)) {
                mHostText.setText(value);
                buddy.setHostName(value);
            }else if(SystemSettings.KEY_NAME[1].equals(key)) {
                mNickNameText.setText(value);
                buddy.setNickName(value);
            }else if(SystemSettings.KEY_NAME[2].equals(key)) {
                mSignatureText.setText(value);
                buddy.setSignature(value);
            }
        }
    }
}
